package org.railage.client.beans;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.log4j.Logger;
import org.railage.common.dto.TripThroughTwoStationsDTO;
import org.railage.server.services.StationService;
import org.railage.server.services.TripService;

@ManagedBean(name = "tripSearch")
@ViewScoped
public class TripSearchBean {
	
	private static final Logger log = Logger.getLogger(TripSearchBean.class);
	
	private StationService stationService = new StationService();
	private TripService tripService = new TripService();
	
	private String selectedStationA = null;
	private String selectedStationB = null;
	private String[] stations;

	private Date departureDate = new Date();
	private Date arrivalDate = new Date();
	private Date duration = new Date();
	private Date currentDate = new Date();
	
	private SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm");
	
	private int radioButtonVal = 1;
	
	private List<TripThroughTwoStationsDTO> trips = null;
	private TripThroughTwoStationsDTO selectedTrip = null;
	
	public TripSearchBean() {
		stations = stationService.getStationNames(null);
	}
	
	public void search() {
		log.debug("Station A: " + selectedStationA);
		log.debug("Station B: " + selectedStationB);
		
		switch (radioButtonVal) {
		case 1:
			trips = Arrays.asList(tripService.getTripsThroughTwoStations(new String[] {selectedStationA, selectedStationB}));
			break;
		case 2:
			String departureDateStr = dateTimeFormatter.format(departureDate) + ":00";
			String arrivalDateStr = dateTimeFormatter.format(arrivalDate) + ":00";
			log.debug("departureDate: " + departureDateStr);
			log.debug("arrivalDate: " + arrivalDateStr);
			trips = Arrays.asList(tripService.getTripsThroughTwoStations(new String[] {selectedStationA, selectedStationB, departureDateStr, arrivalDateStr}));
			break;
		case 3:
			String durationStr = timeFormatter.format(duration) + ":00";
			log.debug("duration: " + durationStr);
			trips = Arrays.asList(tripService.getTripsThroughTwoStations(new String[] {selectedStationA, selectedStationB, durationStr}));
			break;
		default:
			break;
		}
	}

	public String[] getStations() {
		return stations;
	}

	public String getSelectedStationA() {
		return selectedStationA;
	}

	public void setSelectedStationA(String selectedStationA) {
		this.selectedStationA = selectedStationA;
	}

	public String getSelectedStationB() {
		return selectedStationB;
	}

	public void setSelectedStationB(String selectedStationB) {
		this.selectedStationB = selectedStationB;
	}

	public List<TripThroughTwoStationsDTO> getTrips() {
		return trips;
	}

	public void setTrips(List<TripThroughTwoStationsDTO> trips) {
		this.trips = trips;
	}

	public Date getDepartureDate() {
		return departureDate;
	}

	public void setDepartureDate(Date departureDate) {
		this.departureDate = departureDate;
	}

	public Date getArrivalDate() {
		return arrivalDate;
	}

	public void setArrivalDate(Date arrivalDate) {
		this.arrivalDate = arrivalDate;
	}

	public Date getDuration() {
		return duration;
	}

	public void setDuration(Date duration) {
		this.duration = duration;
	}

	public int getRadioButtonVal() {
		return radioButtonVal;
	}

	public void setRadioButtonVal(int radioButtonVal) {
		this.radioButtonVal = radioButtonVal;
	}

	public TripThroughTwoStationsDTO getSelectedTrip() {
		return selectedTrip;
	}

	public void setSelectedTrip(TripThroughTwoStationsDTO selectedTrip) {
		this.selectedTrip = selectedTrip;
	}

	public Date getCurrentDate() {
		return currentDate;
	}
	
}
